package HomePage

import (
	"FromNowOnPromote/博客系统/Tblog/controllers"
	"FromNowOnPromote/博客系统/Tblog/models"
	"FromNowOnPromote/博客系统/Tblog/models/User"
	"encoding/json"
	"log"
)

type HomePage struct {
	controllers.MainController
}

type BlogMessage struct {
	BlogId      int
	BlogTitle   string
	BlogView    int
	BlogUrl     string
	Category    string
	CreateTime  string
	UId         int
	BlogViewUrl string
	BlogDisc    string
	Tbuser      User.TbUser
}

type BlogResp struct {
	BlogId      int    //文章ID
	BlogTitle   string //文章标题
	BlogView    int    //文章阅读人数
	BlogUrl     string //文章路径
	Category    string //文章类型
	CreateTime  string //文章创建时间
	UId         int    //用户ID
	BlogDisc    string
	BlogViewUrl string //文章路径
	Tbuser      userInfo
}

type userInfo struct {
	Username string
	IconUrl  string
}

func (this *HomePage) Get() {
	id, _ := this.GetInt("page")             //获取当前列数
	pagecount, _ := this.GetInt("pagecount") //获取一页的数据列数
	category := this.GetString("category")   //获取当前接口的文章类别
	//进行分页查询
	db := models.ConnDb()
	defer db.Close()
	var bm []BlogMessage
	var br []BlogResp
	limit := (id - 1) * pagecount
	if category == "Home" {
		err1 := db.Raw("select blog_id,blog_title,blog_view,blog_url,category,create_time,uid,blog_disc,blog_view_url from tbloginfo where is_deleted = 0 order by create_time desc limit ?,?", limit, pagecount).Scan(&bm).Error
		if err1 != nil {
			body := map[string]interface{}{"code": 300}
			this.Data["json"] = body
			this.ServeJSON()
			return
		}
		//查询文章对应的用户信息
		for i := 0; i < len(bm); i++ {
			a := bm[i].UId
			err6 := db.Raw("select * from tbuser where uuid=?", a).Scan(&bm[i].Tbuser).Error
			if err6 != nil {
				body := map[string]interface{}{"code": 301}
				this.Data["json"] = body
				this.ServeJSON()
				return
			}
		}
		defer this.ServeJSON()
		b, err7 := json.Marshal(&bm)
		if err7 != nil {
			body := map[string]interface{}{"code": 302}
			this.Data["json"] = body
			this.ServeJSON()
			return
		}
		err7 = json.Unmarshal(b, &br)
		if err7 != nil {
			body := map[string]interface{}{"code": 303}
			this.Data["json"] = body
			this.ServeJSON()
			return
		}
		body := map[string]interface{}{"code": 200, "data": br}
		this.Data["json"] = body
		this.ServeJSON()
		return
	} else {
		err2 := db.Raw("select blog_id,blog_title,blog_view,blog_url,category,create_time,uid,blog_disc,blog_view_url from tbloginfo where category =? and is_deleted = 0 order by create_time desc limit ?,?", category, limit, pagecount).Scan(&bm).Error
		if err2 != nil {
			body := map[string]interface{}{"code": 304}
			this.Data["json"] = body
			this.ServeJSON()
			return
		}
		//查询文章对应的用户信息

		for i := 0; i < len(bm); i++ {
			a := bm[i].UId
			err6 := db.Raw("select * from tbuser where uuid=?", a).Scan(&bm[i].Tbuser).Error
			if err6 != nil {
				body := map[string]interface{}{"code": 301}
				this.Data["json"] = body
				log.Fatal(err6)
			}
		}
		b, err7 := json.Marshal(&bm)
		if err7 != nil {
			body := map[string]interface{}{"code": 306}
			this.Data["json"] = body
			this.ServeJSON()
			return
		}
		err7 = json.Unmarshal(b, &br)
		if err7 != nil {
			body := map[string]interface{}{"code": 307}
			this.Data["json"] = body
			this.ServeJSON()
			return
		}
		body := map[string]interface{}{"code": 200, "data": br}
		this.Data["json"] = body
		this.ServeJSON()
	}
}
